/*--------------------环境模块.预警管理--ly-----------------*/
/*--------------------设备规则信息修改功能  en_warningrule_edit.html------------------*/

$(document).ready(function() {
	//获取cookie:ueserid & Wtid
	var ruleid = $.cookie("ruleid");
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;

	//填充数据
	var searchurl = environmentport + '/RuleController/selectByPrimaryKey'; //查询
	$.get(searchurl, {
		'id': ruleid
	}, function(result) {
		console.log(result);
		var code = result.code;
		var editlist = result.object;
		if(code == 0) {
			$("#L_rulename").val(editlist.rulename); //设备名称
			$("#L_temperatureuUp").val(editlist.temperatureuUp); //温度上限
			$("#L_temperatureuDown").val(editlist.temperatureuDown); //温度下限
			$("#L_humidityUp").val(editlist.humidityUp); //湿度上限
			$("#L_humidityDown").val(editlist.humidityDown); //湿度下限
			$("#L_pm25Up").val(editlist.pm25Up); //PM2.5上限
			$("#L_pm25Down").val(editlist.pm25Down); //PM2.5下限
			$("#L_pm10up").val(editlist.pm10up); //PM10上限
			$("#L_pm10down").val(editlist.pm10down); //PM10下限
			$("#L_noiseup").val(editlist.noiseup); //噪音上限
			$("#L_noisedown").val(editlist.noisedown); //噪音下限
			$("#L_windspeedup").val(editlist.windspeedup); //风速上限
			$("#L_windspeeddown").val(editlist.windspeeddown); //风速下限
			$("#L_windpowerup").val(editlist.windpowerup); //风力等级上限
			$("#L_windpowerdown").val(editlist.windpowerdown); //风力等级下限
		} else {
			console.log("网络不佳，稍后再试！")
		}
	});

	//layui
	layui.use(['form', 'layer'], function() {
		$ = layui.jquery;
		var form = layui.form,
			layer = layui.layer;

		//自定义验证规则
		form.verify({
			name: function(value) {
				if(value.length < 5) {
					return '昵称至少得5个字符啊';
				}
			},

		});

		//获取表单数据
		$("input").blur(function() {
			//失去焦点事件
			var rulename = $("#L_rulename").val(); //设备名称
			var devicetype = $("#L_devicetype").val(); //设备类型
			var temperatureuUp = $("#L_temperatureuUp").val(); //温度上限
			var temperatureuDown = $("#L_temperatureuDown").val(); //温度下限
			var humidityUp = $("#L_humidityUp").val(); //湿度上限
			var humidityDown = $("#L_humidityDown").val(); //湿度下限
			var pm25Up = $("#L_pm25Up").val(); //PM2.5上限
			var pm25Down = $("#L_pm25Down").val(); //PM2.5下限
			var pm10up = $("#L_pm10up").val(); //二氧化碳浓度上限
			var pm10down = $("#L_pm10down").val(); //二氧化碳下限
			var noiseup = $("#L_noiseup").val(); //土壤温度上限
			var noisedown = $("#L_noisedown").val(); //土壤温度下限
			var windspeedup = $("#L_windspeedup").val(); //土壤湿度上限
			var windspeeddown = $("#L_windspeeddown").val(); //土壤湿度下限
			var windpowerup = $("#L_windpowerup").val(); //土壤电导上限
			var windpowerdown = $("#L_windpowerdown").val(); //土壤电导下限 
			//监听提交
			form.on('submit(edit)', function() {
				//递交修改
				editData(rulename, devicetype, temperatureuUp, temperatureuDown, humidityUp, humidityDown, pm25Up, pm25Down, pm10up, pm10down, noiseup, noisedown, windspeedup, windspeeddown, windpowerup, windpowerdown);
				return false;
			});
		});

		//修改用户信息
		var editurl = environmentport + '/RuleController/modifyByDeviceid'; //修改
		// 获得frame索引
		var index = parent.layer.getFrameIndex(window.name);
		var editData = function(rulename, devicetype, temperatureuUp, temperatureuDown, humidityUp, humidityDown, pm25Up, pm25Down, pm10up, pm10down, noiseup, noisedown, windspeedup, windspeeddown, windpowerup, windpowerdown) {
			//修改 edit
			$.get(editurl, {
				ruleid: ruleid,
				rulename: rulename,
				devicetype: devicetype,
				temperatureuUp: temperatureuUp,
				temperatureuDown: temperatureuDown,
				humidityUp: humidityUp,
				humidityDown: humidityDown,
				pm25Up: pm25Up,
				pm25Down: pm25Down,
				pm10up: pm10up,
				pm10down: pm10down,
				noiseup: noiseup,
				noisedown: noisedown,
				windspeedup: windspeedup,
				windspeeddown: windspeeddown,
				windpowerup: windpowerup,
				windpowerdown: windpowerdown
			}, function(result) {
				var code = result.code;
				if(code == 0) {
					layer.msg('修改成功!', {
						icon: 1,
						time: 1000
					}, function() {
						//关闭当前frame
						parent.layer.close(index);
					});
				} else {
					layer.msg('网络开小差了，请稍后再试!', {
						icon: 2,
						time: 1000
					}, function() {
						//关闭当前frame
						//						parent.layer.close(index);
					});
				}
			})
		}

	});
})